package com.baimifan.renovation.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.baimifan.renovation.R;
import com.baimifan.renovation.model.FindData;
import com.baimifan.renovation.view.FindOneHolder;
import com.baimifan.renovation.view.FindTwoHolder;

import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by baimifan on 2017/07/05.
 */

public class FindAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<FindData> mlists;
    private Float w, h;

    public FindAdapter (ArrayList<FindData> list, Float w, Float h) {
        this.mlists = list;
        this.w = w;
        this.h = h;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new FindOneHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.findone_item, parent, false));
        } else if (viewType == 1) {
            return new FindTwoHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.findtwo_item, parent, false));
        } else {
            return new FindOneHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.findone_item, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        FindData findData = mlists.get(position);
        if (holder instanceof FindOneHolder) {
            FindOneHolder oneHolder = ((FindOneHolder)holder);
            LinearLayout.LayoutParams params_iv = new LinearLayout.LayoutParams((int)(w*74), (int)(h*74));
            params_iv.setMargins((int)(w*15), (int)(h*12), (int)(w*15), (int)(h*15));
            oneHolder.iv.setLayoutParams(params_iv);
            RelativeLayout.LayoutParams params_title = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params_title.setMargins(0, (int)(h*12), (int)(w*10), 0);
            oneHolder.title.setLayoutParams(params_title);
            RelativeLayout.LayoutParams params_time = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params_time.setMargins(0, (int)(h*67), (int)(w*10), 0);
            oneHolder.time.setLayoutParams(params_time);
            x.image().bind(oneHolder.iv, findData.getZlPic());
            oneHolder.title.setText(findData.getZlTitle());
            oneHolder.time.setText(findData.getCreateTime());
        } else {
            FindTwoHolder twoHolder = ((FindTwoHolder)holder);
            RelativeLayout.LayoutParams params_title = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            params_title.setMargins((int)(w*15), (int)(h*2), (int)(w*10), (int)(h*22));
            params_title.addRule(RelativeLayout.CENTER_VERTICAL);
            params_title.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            twoHolder.title.setLayoutParams(params_title);

            RelativeLayout.LayoutParams params_time = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            params_time.addRule(RelativeLayout.CENTER_VERTICAL);
            params_time.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params_time.setMargins(0, (int)(h*4), (int)(w*16), (int)(h*25));
            twoHolder.time.setLayoutParams(params_time);

            LinearLayout.LayoutParams params_iv = new LinearLayout.LayoutParams((int)(w*104), (int)(h*104));
            params_iv.setMargins((int)(w*15), 0, 0, (int)(h*10));
            twoHolder.iv1.setLayoutParams(params_iv);
            LinearLayout.LayoutParams params_iv2 = new LinearLayout.LayoutParams((int)(w*104), (int)(h*104));
            params_iv2.setMargins((int)(w*9), 0, (int)(w*9), (int)(h*10));
            twoHolder.iv2.setLayoutParams(params_iv2);
            LinearLayout.LayoutParams params_iv3 = new LinearLayout.LayoutParams((int)(w*104), (int)(h*104));
            params_iv3.setMargins(0, 0, (int)(w*9), (int)(h*10));
            twoHolder.iv3.setLayoutParams(params_iv3);
            twoHolder.title.setText(findData.getZlTitle());
            twoHolder.time.setText(findData.getCreateTime());
            if (findData.getZlPicArray() !=null && findData.getZlPicArray().length == 3) {
                x.image().bind(twoHolder.iv1, findData.getZlPicArray()[0]);
                x.image().bind(twoHolder.iv2, findData.getZlPicArray()[1]);
                x.image().bind(twoHolder.iv3, findData.getZlPicArray()[2]);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mlists.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mlists.get(position).getZlType() == 0) {
            return  0;
        } else if (mlists.get(position).getZlType() == 1) {
            return 1;
        } else {
            return 0;
        }
    }
}
